public class MontyHallExperiment {

    private int N;

    public MontyHallExperiment(int N) {
        if (N < 0) {
            throw new IllegalArgumentException("N 必须大于 0！");
        }

        this.N = N;
    }

    public void run(boolean changeDoor) {
        int wins = 0;
        for (int i = 0; i < N; i ++) {
            if (play(changeDoor)) {
                wins ++;
            }
        }

        System.out.println(changeDoor ? "换门" : "不换门");
        System.out.println("胜率：" + (double) wins / N);
    }

    private boolean play(boolean changeDoor) {
        // Door 0、1、2
        int prizeDoor = (int) (Math.random() * 3);
        int playerChoice = (int) (Math.random() * 3);

        if (playerChoice == prizeDoor) {
            return ! changeDoor;
        } else {
            return changeDoor;
        }
    }

    public static void main(String[] args) {
        int N = 1000000;
        MontyHallExperiment exp = new MontyHallExperiment(N);
        exp.run(true);
        System.out.println();
        exp.run(false);
    }
}
